// 成就系统服务
const store = require('../store')

// 预设成就列表
const PRESET_ACHIEVEMENTS = [
  // 基础成就
  {id: 1, category: '基础', description: '首次游戏', key: 'first_game'},
  {id: 2, category: '基础', description: '赢得第一颗弹珠', key: 'first_win'},
  {id: 3, category: '基础', description: '完成10局游戏', key: 'play_10_games'},
  
  // 社交成就
  {id: 4, category: '社交', description: '邀请1位好友', key: 'invite_1_friend'},
  {id: 5, category: '社交', description: '赠送1颗弹珠', key: 'gift_1_marble'},
  {id: 6, category: '社交', description: '收到1颗弹珠', key: 'receive_1_marble'},
  
  // 复仇成就  
  {id: 7, category: '复仇', description: '复仇成功1次', key: 'revenge_1'},
  {id: 8, category: '复仇', description: '连续复仇成功3次', key: 'revenge_streak_3'},
  
  // 收集成就
  {id: 9, category: '收集', description: '拥有5颗弹珠', key: 'collect_5_marbles'},
  {id: 10, category: '收集', description: '拥有10颗弹珠', key: 'collect_10_marbles'},
  
  // 技能成就
  {id: 11, category: '技能', description: '一杆进洞', key: 'hole_in_one'},
  {id: 12, category: '技能', description: '连续3次一杆进洞', key: 'hole_in_one_streak_3'},
  
  // 地形成就
  {id: 13, category: '地形', description: '在雪地获胜', key: 'win_on_snow'},
  {id: 14, category: '地形', description: '在沙漠获胜', key: 'win_on_desert'},
  
  // 天气成就
  {id: 15, category: '天气', description: '在雨天获胜', key: 'win_in_rain'},
  {id: 16, category: '天气', description: '在雪天获胜', key: 'win_in_snow'},
  
  // 特殊成就
  {id: 17, category: '特殊', description: '绝地反击', key: 'comeback'},
  {id: 18, category: '特殊', description: '完美胜利', key: 'perfect_win'},
  {id: 19, category: '特殊', description: '弹珠大师', key: 'marble_master'},
  {id: 20, category: '特殊', description: '终极收藏家', key: 'ultimate_collector'}
]

class AchievementService {
  constructor() {
    this.achievements = PRESET_ACHIEVEMENTS
    this.userAchievements = store.state.achievement.userAchievements || {}
  }

  // 检查成就完成状态
  checkAchievement(key) {
    if (!this.userAchievements[key]) {
      this.userAchievements[key] = {
        completed: false,
        count: 0,
        timestamp: null
      }
    }
    return this.userAchievements[key]
  }

  // 更新成就进度
  updateAchievement(key, count = 1) {
    const achievement = this.checkAchievement(key)
    achievement.count += count
    
    // 检查是否首次完成
    if (!achievement.completed && 
        this.isRequirementMet(key, achievement.count)) {
      achievement.completed = true
      achievement.timestamp = Date.now()
      this.triggerNotification(key)
      return true
    }
    return false
  }

  // 检查成就条件是否满足
  isRequirementMet(key, count) {
    // 这里根据成就类型实现具体条件判断
    // 例如: 赢得弹珠数量、邀请好友数量等
    return true
  }

  // 触发成就通知
  triggerNotification(key) {
    const achievement = this.achievements.find(a => a.key === key)
    if (achievement) {
      uni.showToast({
        title: `成就解锁: ${achievement.description}`,
        icon: 'none'
      })
    }
  }

  // 获取所有成就状态
  getAllAchievements() {
    return this.achievements.map(ach => ({
      ...ach,
      status: this.userAchievements[ach.key] || {
        completed: false,
        count: 0,
        timestamp: null
      }
    }))
  }
}

module.exports = new AchievementService()
